package haizhi.fruitmall.service.impl;

import haizhi.fruitmall.constant.UserDetailedMsg;
import haizhi.fruitmall.constant.UserMsg;
import haizhi.fruitmall.dao.mapper.AuthMapper;
import haizhi.fruitmall.dao.mapper.RoleAuthMapper;
import haizhi.fruitmall.dao.mapper.RoleMapper;
import haizhi.fruitmall.dao.mapper.UserDetailedMapper;
import haizhi.fruitmall.dao.mapper.UserMapper;
import haizhi.fruitmall.dao.mapper.UserRoleMapper;
import haizhi.fruitmall.pojo.po.Auth;
import haizhi.fruitmall.pojo.po.Role;
import haizhi.fruitmall.pojo.po.RoleAuth;
import haizhi.fruitmall.pojo.po.RoleAuthExample;
import haizhi.fruitmall.pojo.po.User;
import haizhi.fruitmall.pojo.po.UserDetailed;
import haizhi.fruitmall.pojo.po.UserDetailedExample;
import haizhi.fruitmall.pojo.po.UserExample;
import haizhi.fruitmall.pojo.po.UserRole;
import haizhi.fruitmall.pojo.po.UserRoleExample;
import haizhi.fruitmall.service.UserService;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private UserDetailedMapper userDetailedMapper;

	@Autowired
	private RoleMapper roleMapper;

	@Autowired
	private UserRoleMapper userRoleMapper;

	@Autowired
	private AuthMapper authMapper;

	@Autowired
	private RoleAuthMapper roleAuthMapper;

	/**
	 * 增加
	 */
	@Override
	public void insertUser(User user) {
		userMapper.insertSelective(user);
	}

	@Override
	public void insertUserDetailed(UserDetailed userDetailed) {
		userDetailedMapper.insert(userDetailed);
	}

	@Override
	public void insertUserRole(UserRole userRole) {
		userRoleMapper.insert(userRole);
	}

	/**
	 * 删除
	 */
	@Override
	public void deleteUserById(Integer user_id) {
		userMapper.deleteByPrimaryKey(user_id);
	}

	@Override
	public void deleteUserDetailedById(Integer user_id) {
		userDetailedMapper.deleteByPrimaryKey(user_id);
	}

	@Override
	public void deleteUserRoleById(Integer user_id) {
		UserRoleExample userRoleExample = new UserRoleExample();
		UserRoleExample.Criteria criteria = userRoleExample.createCriteria();
		criteria.andUserIdEqualTo(user_id);
		userRoleMapper.deleteByExample(userRoleExample);
	}

	public void deleteUserByUserName(String user_name) {
		UserExample userExample = new UserExample();
		UserExample.Criteria criteria = userExample.createCriteria();
		criteria.andUserNameEqualTo(user_name);
		userMapper.deleteByExample(userExample);
	}

	/**
	 * 修改
	 */
	@Override
	public void updateUser(User user, String user_name) {
		UserExample userExample = new UserExample();
		UserExample.Criteria criteria = userExample.createCriteria();
		criteria.andUserNameEqualTo(user_name);
		userMapper.updateByExampleSelective(user, userExample);
	}

	@Override
	public void updateUserTel(UserDetailed userDetailed, String user_name) {
		UserDetailedExample userDetailedExample = new UserDetailedExample();
		UserDetailedExample.Criteria criteriadetailed = userDetailedExample
				.createCriteria();
		criteriadetailed.andUserIdEqualTo(getUserIdByUsername(user_name));
		userDetailedMapper.updateByExampleSelective(userDetailed,
				userDetailedExample);
	}

	/**
	 * 查询
	 */
	@Override
	public User findUserById(Integer id) {
		return userMapper.selectByPrimaryKey(id);
	}

	@Override
	public PageInfo getPageInfo(int currentPage, int pageSize) {
		PageHelper.startPage(currentPage, pageSize);
		UserExample userExample = new UserExample();
		UserExample.Criteria criteria = userExample.createCriteria();
		userExample.setOrderByClause("user_id desc");
		List<User> userlist = userMapper.selectByExample(userExample);
		return new PageInfo(userlist);
	}

	@Override
	public List<UserDetailed> getUserDetailedByCondition(Map map) {
		UserDetailedExample userDetailedExample = new UserDetailedExample();
		UserDetailedExample.Criteria criteria = userDetailedExample
				.createCriteria();
		Integer user_id = (Integer) map.get(UserMsg.USER_ID);
		String real_name = (String) map.get(UserDetailedMsg.USER_REAL_NAME);
		String register_time = (String) map
				.get(UserDetailedMsg.USER_REGISTER_TIME);
		String user_tel = (String) map.get(UserDetailedMsg.USER_TEL);
		if (user_id != null)
			criteria.andUserIdEqualTo(user_id);
		if (real_name != null)
			criteria.andRealNameEqualTo(real_name);
		if (register_time != null)
			criteria.andRegisterTimeEqualTo(register_time);
		if (user_tel != null)
			criteria.andUserTelEqualTo(user_tel);
		return userDetailedMapper.selectByExample(userDetailedExample);
	}

	@Override
	public UserDetailed getUserDetailedByName(String user_name) {
		Integer user_id = getUserIdByUsername(user_name);
		return userDetailedMapper.selectByPrimaryKey(user_id);
	}

	@Override
	public List<User> getUserByUserName(String user_name) {
		UserExample userExample = new UserExample();
		UserExample.Criteria criteria = userExample.createCriteria();
		criteria.andUserNameEqualTo(user_name);
		return userMapper.selectByExample(userExample);
	}

	@Override
	public String getPasswordByUserName(String user_name) {
		UserExample userExample = new UserExample();
		UserExample.Criteria criteria = userExample.createCriteria();
		criteria.andUserNameEqualTo(user_name);
		return userMapper.selectByExample(userExample).get(0).getUserPassword();
	}

	@Override
	public Integer getUserIdByUsername(String user_name) {
		UserExample userExample = new UserExample();
		UserExample.Criteria criteria = userExample.createCriteria();
		criteria.andUserNameEqualTo(user_name);
		return userMapper.selectByExample(userExample).get(0).getUserId();
	}

	@Override
	public Set<String> getUserRoleSet(String user_name) {
		Set<String> userRoleSet = new HashSet<String>();
		Integer user_id = getUserIdByUsername(user_name);
		List<UserRole> UserRoleList = getUserRoleList(user_id);
		for (UserRole userRole : UserRoleList) {
			userRoleSet.add(getRoleById(userRole.getRoleId()).getRoleName());
		}
		return userRoleSet;
	}

	@Override
	public Role getRoleById(Integer role_id) {
		return roleMapper.selectByPrimaryKey(role_id);
	}

	@Override
	public Set<String> getUserAuthSet(String user_name) {
		Set<String> userAuthSet = new HashSet<String>();
		Integer user_id = getUserIdByUsername(user_name);
		List<UserRole> UserRoleList = getUserRoleList(user_id);
		for (UserRole userRole : UserRoleList) {
			List<RoleAuth> roleAuthList = getRoleAuthList(userRole.getRoleId());
			for (RoleAuth roleAuth : roleAuthList) {
				Auth auth = getAuthById(roleAuth.getAuthId());
				System.out.println("authtest: " + auth);
				if (auth.getAuthA() != null || !auth.getAuthA().equals("")
						|| !auth.getAuthA().equals(null)) {
					userAuthSet.add(auth.getAuthC() + ":" + auth.getAuthA());
				}
			}
		}
		return userAuthSet;
	}

	@Override
	public List<UserRole> getUserRoleList(Integer user_id) {
		UserRoleExample userRoleExample = new UserRoleExample();
		UserRoleExample.Criteria criteria = userRoleExample.createCriteria();
		criteria.andUserIdEqualTo(user_id);
		return userRoleMapper.selectByExample(userRoleExample);
	}

	@Override
	public List<RoleAuth> getRoleAuthList(Integer role_id) {
		RoleAuthExample roleAuthExample = new RoleAuthExample();
		RoleAuthExample.Criteria criteria = roleAuthExample.createCriteria();
		criteria.andRoleIdEqualTo(role_id);
		return roleAuthMapper.selectByExample(roleAuthExample);
	}

	@Override
	public Auth getAuthById(Integer auth_id) {
		return authMapper.selectByPrimaryKey(auth_id);
	}
}
